package com.zhu.solution.linkQuestion;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhujiabao
 * @Date: 2021/12/28/9:48
 * @Description:
 */
public class Solution_3 {
    public static void main(String[] args) {
        int[] head = {1,2,3,4,5};

        //链表头节点
        ListNode headNode = new ListNode(head[0]);
        ListNode tempNode = headNode;
        //新建链表
        for (int i=1; i<head.length; i++){
            tempNode.next = new ListNode(head[i]);
            tempNode = tempNode.next;
        }

        //打印链表
        System.out.print("listNode");
        tempNode = headNode;
        while (tempNode != null){
            System.out.print(" --> " + tempNode.val);
            tempNode = tempNode.next;
        }

        //反转链表
        ListNode headTemp = new Solution_3().reverseList(headNode);
//        ListNode headTemp = new Solution_3().reverseList_2(headNode);

        System.out.println();
        //打印链表
        System.out.print("listNode");
        tempNode = headTemp;
        while (tempNode != null){
            System.out.print(" --> " + tempNode.val);
            tempNode = tempNode.next;
        }


    }

    public ListNode reverseList_2(ListNode head) {
        if (head.next==null || head==null)
            return head;
        ListNode headTemp = reverseList_2(head);
        headTemp.next = head;

        return reverseList_2(head);
    }

    public ListNode reverse(ListNode head,ListNode headTemp) {
        if (headTemp.next==null){
            head = headTemp;
            return headTemp;
        }

        ListNode tempNode = reverse(head,headTemp.next);

        head = headTemp;

        return headTemp;
    }

    public ListNode reverseList(ListNode head) {

        if (head==null || head.next==null)
            return head;
        ListNode indexNode = head;
        ListNode headNew = null;
        ListNode tempNode ;
        while (indexNode!=null){
            tempNode = new ListNode(indexNode.val);
            tempNode.next = headNew;
            headNew = tempNode;
            indexNode = indexNode.next;
        }
        return headNew;
    }

    static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
